﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Coslen.TahorCRM.Domain.Model.Entities;
using Coslen.TahorCRM.Domain.Repositories.Interfaces;

namespace Coslen.TahorCRM.Security
{
    public class TahorRoleProvider : RoleProvider
    {
        public IUserRepository UserRepository;  

        public TahorRoleProvider() : this(null)        
        { }

        public TahorRoleProvider(IUserRepository repository)
            : base()
        {
            UserRepository = repository; //?? UserRepositoryFactory.GetRepository();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            User user = UserRepository.GetUserByUserName(username);
            
            if (user != null)
            {
                TahorPrincipal tahorPrincipal = new TahorPrincipal(user, false);
                return tahorPrincipal.IsInRole(roleName); 
            }
            
            return false;
        }
        
        public override string[] GetRolesForUser(string username)
        {
            User user = UserRepository.GetUserByUserName(username);
            List<string> userRoles = new List<string>();
            
            // Note that roles + role rights is the complete list...
            foreach (UserRole userRole in user.UserRoles)
            {
                userRoles.Add(userRole.Role.Name);
                foreach (RoleRight roleRight in userRole.Role.RoleRights)
                {
                    userRoles.Add(roleRight.Right.Name);
                }
            }
            
            return userRoles.Distinct().ToArray();
        }

        #region Non Implemented Methods
        
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion

    }
}
